﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FDEE.Model
{

    public class PileCapMHK : FootingBase
    {
        int DistanceOfPile;
        int NumPile;
        int SideLength;
        public PileCapMHK(EFootingType _ePCType, string typeName, string pierNumber, eSubType st,
            int width, int length, int height, int distPile, int numPile, int side) : base(_ePCType, typeName, pierNumber, st, width, length, height)
        {
            DistanceOfPile = distPile;
            NumPile = numPile;
            SideLength = side;
        }

        public override void SetConcreteClass()
        {
            base.SetConcreteClass();
        }

        public override void SetConcreteVolume()
        {
            base.SetConcreteVolume();
        }

        public override void SetRebarList()
        {
            int diaOfN1 = 16;

            var ResN1 = GetSplice(Length, diaOfN1, 500, 1200, 12000, 75 + 16);
            var ResN5 = GetSplice(Length, diaOfN1, 500, 1200, 12000, 75);
            if (ResN1[0] == 0)
            {
                RebarList.Add(new MIRebarRecord(1, 0, "", eDB.DB16, Length, 75 + 16, 500, 500, 4, eShapeType.MI));
                RebarList.Add(new MIRebarRecord(3, 0, "", eDB.DB16, Length, 75 + 16, 500, 500, 4, eShapeType.MI));
                RebarList.Add(new MIRebarRecord(5, 0, "", eDB.DB16, Length, 75, 500, 500, 4, eShapeType.MI));
            }
            else
            {
                var rr = 3 * 16;
                RebarList.Add(new MLADDRebarRecord(1, 1, "", eDB.DB16, 4, eShapeType.MLL, 500, ResN1[1] - 500 + rr / 2 + diaOfN1, rr));
                RebarList.Add(new MLADDRebarRecord(3, 1, "", eDB.DB16, 4, eShapeType.MLL, 500, ResN1[1] - 500 + rr / 2 + diaOfN1, rr));
                RebarList.Add(new MLADDRebarRecord(5, 1, "", eDB.DB16, 4, eShapeType.MLL, 500, ResN5[1] - 500 + rr / 2 + diaOfN1, rr));

                RebarList.Add(new MLADDRebarRecord(1, 2, "", eDB.DB16, 4, eShapeType.MLR, 500, ResN1[2] - 500 + rr / 2 + diaOfN1, rr));
                RebarList.Add(new MLADDRebarRecord(3, 2, "", eDB.DB16, 4, eShapeType.MLR, 500, ResN1[2] - 500 + rr / 2 + diaOfN1, rr));
                RebarList.Add(new MLADDRebarRecord(5, 2, "", eDB.DB16, 4, eShapeType.MLR, 500, ResN5[2] - 500 + rr / 2 + diaOfN1, rr));

                if (ResN1[0] >= 2)
                {
                    RebarList.Add(new MARebarRecord(1, 3, "", eDB.DB16, 12000, 4 * (ResN1[0] - 1), eShapeType.MA));
                    RebarList.Add(new MARebarRecord(3, 3, "", eDB.DB16, 12000, 4 * (ResN1[0] - 1), eShapeType.MA));
                    RebarList.Add(new MARebarRecord(5, 3, "", eDB.DB16, 12000, 4 * (ResN5[0] - 1), eShapeType.MA));
                }
            }

            int NoN2N4 = (Length - 75 * 2) / 300 + 1;
            RebarList.Add(new MIRebarRecord(2, 0, "", eDB.DB12, 800, 75, 600 - 116, 600 - 116, NoN2N4, eShapeType.MI));
            RebarList.Add(new MIRebarRecord(4, 0, "", eDB.DB12, 800, 75, 300, 300, NoN2N4, eShapeType.MI));


            RebarList.Sort();
        }


        int[] GetSplice(int L, int db, int A, int lap, int tryL1, int cover)
        {
            int r = db * 3;
            int B = L - cover * 2;
            int LengthRaw = A + B + A - r - 2 * db;
            int L1 = 0;
            int L2 = 0;
            int StandardLen = 12000;
            int n;
            if (LengthRaw < StandardLen)
            {
                L1 = LengthRaw;
                L2 = L1;
                n = 0;
                return new int[] { n, L1, L2 };
            }
            else
            {
                // 需要切分
                if ((LengthRaw + lap + lap) * 0.5 < StandardLen)
                {
                    // 1个接头
                    // lenraw+lap=L1+L2
                    // L1-L2=lap

                    L1 = (int)((LengthRaw + lap + lap) * 0.5);
                    L2 = L1 - lap;
                    n = 1;
                }
                else
                {
                    L1 = tryL1;
                    int M1 = L1 - A + db + r / 2;
                    n = (B - M1 - lap / 2) / (StandardLen - lap) + 1;
                    int M2 = B - M1 + lap / 2 - (n - 1) * (StandardLen - lap) + lap / 2;
                    L2 = A + M2 - r / 2 - db;
                    if (M2 < 500 || M2 > M1 - lap)
                    {
                        if (tryL1 == 12000)
                        {
                            return GetSplice(L, db, A, lap, 9000, cover);
                        }
                        else
                        {
                            throw new Exception();
                        }
                    }
                }
                return new int[] { n, L1, L2 };

            }

        }

    }
}
